package conterlles

import (
	"day12_10_homework/models/mysql"
	"day12_10_homework/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
)

type JsonRes struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func UserRegister(c *gin.Context) {
	tel := c.PostForm("tel")
	password := c.PostForm("password")
	if tel == "" || password == "" {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "不能为空",
			Data:    nil,
		})
		return
	}
	if !pkg.Tel(tel) {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "手机号格式错误",
			Data:    nil,
		})
		return
	}
	password1 := password + "1234"
	password2 := pkg.Md5(password1)
	user := mysql.Users{
		Tel:      tel,
		Password: password2,
	}
	err := user.Register()
	if err != nil {
		c.JSON(http.StatusOK, JsonRes{
			Code:    500,
			Message: "注册失败",
			Data:    nil,
		})
		return
	}
	c.JSON(http.StatusOK, JsonRes{
		Code:    200,
		Message: "注册成功",
		Data:    user.ID,
	})
}
